from langchain_core.tools import tool

insurance_orders = {
    "水滴接好孕·母婴保3.0（优享版）": {
        "长短险类型": "短险",
        "保险产品名称": "水滴接好孕·母婴保3.0（优享版）",
        "是否可水滴正常退": True,
        "投保人身份证后四位": "7241",
        "保单状态": 4,
        "正常退保预计退费金额": 100,
        "手机号": "13812345678",
        "姓名": "张三",
        "用户ID": "1234567890",
        "订单号": "order_1"
    }, "水滴全能保癌无忧医疗险（焕新版）": {
        "长短险类型": "短险",
        "保险产品名称": "水滴全能保癌无忧医疗险（焕新版）",
        "是否可水滴正常退": False,
        "投保人身份证后四位": "7241",
        "保单状态": 4,
        "正常退保预计退费金额": 200,
        "手机号": "13812345678",
        "姓名": "张三",
        "用户ID": "1234567890",
        "订单号": "order_2"
    }, "水滴蓝海1号重疾险": {
        "长短险类型": "短险",
        "保险产品名称": "水滴蓝海1号重疾险",
        "是否可水滴正常退": True,
        "投保人身份证后四位": "122X",
        "保单状态": 4,
        "正常退保预计退费金额": 300,
        "手机号": "13812345678",
        "姓名": "李四",
        "用户ID": "1234567891",
        "订单号": "order_2"
    }, "水滴老年医疗险": {
        "长短险类型": "短险",
        "保险产品名称": "水滴老年医疗险",
        "是否可水滴正常退": False,
        "投保人身份证后四位": "3527",
        "保单状态": 4,
        "正常退保预计退费金额": 132.39,
        "手机号": "13812345678",
        "姓名": "王五",
        "用户ID": "1234567892",
        "订单号": "order_3"
    }
}


# 查询保单
@tool
def insurance_order_search(user_id: str) -> str:
    """查询用户在途的保单，当用户提供了上述信息之后，必须调用该方法获取保单信息"""
    result = ""
    index = 1
    for key, value in insurance_orders.items():
        if value.get("用户ID", "") == user_id:
            result = result + str(index) + "、保险产品名称：" + value['保险产品名称'] + '\n'
            index = index + 1
    return result if result else "暂无查询到您的保单"


@tool
def is_insurance_order_refundable(insurance_name: str) -> str:
    """根据保险名称判断用户选择要退的当前保单是否AI可退"""
    insurance_order = insurance_orders.get(insurance_name, None)
    if not insurance_order:
        return "当前保单不存在"
    if insurance_order.get("是否可水滴正常退", False):
        return "当前保单可退"
    return "当前保单不可退，需要联系对应保司进行操作"


@tool
def work_order_create() -> str:
    """给用户创建工单"""
    return "创建工单成功"


@tool
def insurance_order_cancel_amount(insurance_name: str) -> str:
    """根据用户选择退保的保单名称获取可退款金额"""
    insurance_order = insurance_orders.get(insurance_name)
    if not insurance_order:
        return "当前保单不存在"
    return str(insurance_order.get("正常退保预计退费金额"))


@tool
def insurance_order_cancel_authentication(name: str, id_no: str, insurance_name: str):
    """根据用户提供的姓名、身份证号后四位和用户选择要退的保单名称，进行身份验证"""
    insurance_order = insurance_orders.get(insurance_name, None)
    if not insurance_order:
        return "当前保单不存在"
    if insurance_order.get("姓名") == name and insurance_order.get("投保人身份证后四位") == id_no:
        return "身份验证通过"
    return "身份验证失败"


def insurance_order_type(insurance_name: str) -> str:
    """根据用户选择的保单名称进行所退保单的品类的判断"""
    insurance_order = insurance_orders.get(insurance_name, None)
    if not insurance_order:
        return "当前保单不存在"
    return insurance_order.get("长短险类型")


# 保单退保
@tool
def insurance_order_cancel_long(insurance_name: str) -> str:
    """根据用户选择的保单名称进行长险退保操作"""
    insurance_order = insurance_orders.get(insurance_name, None)
    if not insurance_order:
        return "当前保单不存在"
    return "http://www.baidu.com，请访问前面的链接进行退保。请问还有什么需要帮助的吗？"


@tool
def insurance_order_cancel_short(insurance_name: str) -> str:
    """根据用户选择的保单名称进行短险退保操作"""
    insurance_order = insurance_orders.get(insurance_name, None)
    if not insurance_order:
        return "当前保单不存在"
    return "已退保成功，请问还有什么需要帮助的吗？"


@tool
def save_reason(reason: str):
    """保存用户提供的退保原因"""
    return "保存退保原因成功"
